package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.util.ObjectUtil;
import com.zhijian.medical.dao.local.PersonTrainTestMapper;
import com.zhijian.medical.entity.po.PersonTrainTest;
import com.zhijian.medical.entity.vo.request.PersonTrainTestSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.PersonTrainTestSearchVo;
import com.zhijian.medical.entity.vo.response.PersonTrainTestVo;
import com.zhijian.medical.service.PersonTrainTestService;
import com.zhijian.medical.util.JsonUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 分子病理室人员岗前培训与考核表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-07-18 14:21:25
 */
@Slf4j
@Service
public class PersonTrainTestServiceImpl implements PersonTrainTestService {

    @Autowired
    private PersonTrainTestMapper mapper;

    @Override
    public PersonTrainTest selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonTrainTest selectByPersonId(Long personId) {
        return mapper.selectByPersonId(personId);
    }

    @Override
    public PersonTrainTestVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public PersonTrainTestVo selectVoByPersonId(Long personId) {
        PersonTrainTestVo personTrainTestVo = mapper.selectVoByPersonId(personId);
        if (ObjectUtil.isNull(personTrainTestVo)) {
            return null;
        }
        //检测项目内容
        String content = personTrainTestVo.getContent();
        if (StringUtils.isNotBlank(content)) {
            personTrainTestVo.setContentList(JsonUtil.toArray(content, PersonTrainTestSaveOrUpdateVo.ContentVo.class));
        }
        return personTrainTestVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(PersonTrainTest personTrainTest) {
        Date now = new Date();
        personTrainTest.setCreateTime(now);
        personTrainTest.setUpdateTime(now);
        return mapper.insert(personTrainTest) > 0 ? personTrainTest.getId() : 0L;
    }

    @Override
    public boolean update(PersonTrainTest personTrainTest) {
        Date now = new Date();
        personTrainTest.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personTrainTest) != 0;
    }

    @Override
    public boolean saveOrUpdate(PersonTrainTestSaveOrUpdateVo personTrainTestSaveOrUpdateVo) {
        PersonTrainTest old = this.selectByPersonId(personTrainTestSaveOrUpdateVo.getPersonId());
        if (ObjectUtil.isNull(old)) {
            //保存
            PersonTrainTest personTrainTest = new PersonTrainTest();
            BeanUtils.copyProperties(personTrainTestSaveOrUpdateVo, personTrainTest);
            //内容
            List<PersonTrainTestSaveOrUpdateVo.ContentVo> contentList = personTrainTestSaveOrUpdateVo.getContentList();
            if (CollectionUtil.isNotEmpty(contentList)) {
                personTrainTest.setContent(JsonUtil.toString(contentList));
            }
            return this.insert(personTrainTest) != 0;
        } else {
            //修改
            personTrainTestSaveOrUpdateVo.setId(old.getId());
            PersonTrainTest personTrainTest = new PersonTrainTest();
            BeanUtils.copyProperties(personTrainTestSaveOrUpdateVo, personTrainTest);
            List<PersonTrainTestSaveOrUpdateVo.ContentVo> contentList = personTrainTestSaveOrUpdateVo.getContentList();
            if (CollectionUtil.isNotEmpty(contentList)) {
                personTrainTest.setContent(JsonUtil.toString(contentList));
            }
            return this.update(personTrainTest);
        }
    }

    @Override
    public boolean updateByPrimaryKeySelective(PersonTrainTest record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(PersonTrainTestSearchVo personTrainTestSearchVo) {
        return mapper.count(personTrainTestSearchVo);
    }

    @Override
    public List<PersonTrainTestVo> selectList(PersonTrainTestSearchVo personTrainTestSearchVo) {
        return mapper.selectList(personTrainTestSearchVo);
    }

    @Override
    public Page<PersonTrainTestVo> selectPage(PersonTrainTestSearchVo personTrainTestSearchVo) {
        //先直接查总数
        int count = count(personTrainTestSearchVo);
        //分页
        Page<PersonTrainTestVo> page = new Page<>(true, count, personTrainTestSearchVo.getPageNum(), personTrainTestSearchVo.getPageSize());
        personTrainTestSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonTrainTestVo> responseVos = this.selectList(personTrainTestSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<PersonTrainTest> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    public static final String CODE_PREFIX = "BLK-SOP-007-02-004";

    @Override
    public String selectMaxCode() {
        return mapper.selectMaxCodeByDay(DateUtil.formatDate(new Date()));
    }

    @Override
    public String createNextCode() {
        String maxCode = this.selectMaxCode();
        if (StringUtils.isBlank(maxCode)) {
            return String.format("%s-%s-1", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")));
        }
        String[] maxCodeArray = maxCode.split("-");
        Integer i = Integer.valueOf(maxCodeArray[maxCodeArray.length - 1]);
        i++;
        return String.format("%s-%s-%s", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")), i);
    }

}